<?php
namespace Admin\Controller;
use Think\Controller;
class StudentController extends AclController {
    // PC 获取用户
    public function getStudent($page=1,$rows=20,$username=''){
    	$user=M('student');
        //筛选条件
        $condition=array();
        if(!empty($username)){
            $condition['stuname']=array('like','%'.$username.'%');
        }
        //取哪几条
        $limit=($page-1)*$rows.','.$rows;
        //总条数
        $count=$user->where($condition)->count();
        //行数据
    	$data=$user->where($condition)->order('id desc')->limit($limit)->select();
    	// echo $user->getLastSql();
        $this->ajaxReturn(array('total'=>$count,'rows'=>$data));
    }


    function addStudent($us=''){    	
        // p($_FILES);

        $stu=M('student');
        $stu->create();
        if($_FILES){
            if($_FILES['idfront']['tmp_name']){
                $stu->idfront=getBase64Pic($_FILES['idfront']['tmp_name']);
            }
            if($_FILES['idback']['tmp_name']){
                $stu->idback=getBase64Pic($_FILES['idback']['tmp_name']);
            }
        }
        $stu->inserttime=time();
        $stu->lastupdatetime=time();

        $res=$stu->add();
    	echo $res;
    }
    
    function savePhoto(){
        $id=$_POST['id'];
        $field=$_POST['fob'];
        $img='';
        if($_FILES){
            if($_FILES['photo']['tmp_name']){
                $img=getBase64Pic($_FILES['photo']['tmp_name']);
            }
        }
        $res=M('student','','DB_LOCAL')->where(array('id'=>$id))->setField("$field",$img);
        echo $res;
    }

    function updateStudent($us=''){
        // p($_POST);
        // exit;
    	//用户登陆信息
        $stu=M('student');
        $data['stuname']=$_POST['stuname'];
        $data['sex']=$_POST['sex'];
        $data['identityid']=$_POST['identityid'];
        $data['phone']=$_POST['phone'];
        $data['qq']=$_POST['qq'];
        $data['weixin']=$_POST['weixin'];
        $data['weixinid']=$_POST['weixinid'];
        $data['lastupdatetime']=time();

    	$res=$stu->where(array('id'=>$_POST['id']))->data($data)->save();

    	echo $res;
    }

    function deleteStudent($id=0){
    	$stu=M('student');
    	$res=$stu->delete($id);
    	echo $res;
    }

    function abandonStudent($id=0){
        $stu=M('student');
        $res=$stu->where('id='.$id)->setField('isdel',1);
        echo $res;
    }


    function activeStudent($id=0){
        $stu=M('student');
        $res=$stu->where('id='.$id)->setField('isdel',0);
        echo $res;
    }


    function resetPwd($id=0){

        $user=M('user');
        $resetpwd=M('sys_config')->where(array('key'=>'resetPwd'))->getField('value');

        $res=$user->where(array('id'=>$id))->data(array('userpwd'=>md5($resetpwd)))->save();         
        echo $res;
    }

    function printContract($id){
        $stu=M('student')->find($id);
        $stu['now']=time();
        $this->assign('stu',$stu);
        $this->display('printContract'.$stu['class']);
    }
}